package com.snake.mylibrary.runtime;

import android.content.Context;
import android.content.pm.ApplicationInfo;
import android.os.Build;
import android.telephony.TelephonyManager;
import android.text.TextUtils;


public final class DeviceInfo
{
	private static String systemVersion;
	private static int sdkVersion;
	private static boolean isSystemApp;//该客户端是否是系统APP
	private static int density;
	private static String imei;
	private static String imsi;
	private static String userAgent;
	private static int screenWidth ;
	private static int screenHeight ;
	private static String carrier;
	private static String model;
	private static String brand;
	private static int version;
	
	public static void init( Context context )
	{
		systemVersion = Build.VERSION.INCREMENTAL;
		sdkVersion = Build.VERSION.SDK_INT;
		isSystemApp = ((context.getApplicationInfo().flags & ApplicationInfo.FLAG_SYSTEM) > 0);
		
		try {
			version = context.getPackageManager().getPackageInfo(context.getPackageName(),0).versionCode;
		} catch (Exception e) {
			e.printStackTrace();
		}
		
		
		density = context.getResources().getDisplayMetrics().densityDpi;
		screenWidth = context.getResources().getDisplayMetrics().widthPixels;
		screenHeight = context.getResources().getDisplayMetrics().heightPixels;
		TelephonyManager tm = (TelephonyManager) context.getSystemService(Context.TELEPHONY_SERVICE);
		model = Build.MODEL;
		brand = Build.BRAND;
		if(tm != null){
			imei = tm.getDeviceId();
			imsi = tm.getSubscriberId();
		}
		get_phone_ua(context);
		
		carrier = getCarriar(tm);
		
		
	}

	/**
	 * UserAgent:厂商|型号|SDK的VersionName|代号|SDK版本
	 */
	public static String get_phone_ua(Context c) {
		// if (userAgent == null) {
		// StringBuffer sb = new StringBuffer();
		// sb.append(Build.MANUFACTURER);//制造商
		// sb.append("/");
		// sb.append(Build.MODEL);//手机型号
		// sb.append("/");
		// sb.append(Build.VERSION.RELEASE);
		// sb.append("/");
		// sb.append(Build.DISPLAY);
		// sb.append("/");
		// sb.append(Build.VERSION.SDK_INT);
		// userAgent = sb.toString();
		// }
		// return userAgent;

		if (userAgent == null) {
			StringBuffer sb = new StringBuffer();
			sb.append(Build.BRAND)
			.append('_')
			.append(Build.MODEL);
			
			userAgent = sb.toString();
		}
		return userAgent;

	}

	
	
	private static String getCarriar(TelephonyManager tm)
	{
		if(tm == null)
		{
			return "";
		}
		if(tm.getSimState() != TelephonyManager.SIM_STATE_READY)
		{
			return "";
		}
		String on = tm.getSimOperatorName();
		int mnc = get_mnc(on);
		String ret = "unkown";
		if(0 == mnc || 2 == mnc || 7 == mnc)
		{
			ret = "cmcc";
		}
		else if(1 == mnc)
		{
			ret = "unicom";
		}
		else if(3 == mnc)
		{
			ret = "telecom";
		}
		return ret;
	}
	
	private static int get_mnc(String numeric){
		if(TextUtils.isEmpty(numeric)){
			return -1;
		}
		if (numeric.length() < 5) {
			return -1;
		}
		int ret = -1;
		try {
			ret = Integer.parseInt(numeric.substring(numeric.length()
					- (numeric.length() > 5 ? 3 : 2)));
		} catch (Exception e) {
		}
		return ret;
	}

	public static String getSystemVersion() {
		return systemVersion;
	}

	public static int getSdkVersion() {
		return sdkVersion;
	}

	public static boolean isSystemApp() {
		return isSystemApp;
	}

	public static int getDensity() {
		return density;
	}

	public static String getImei() {
		return imei;
	}

	public static String getImsi() {
		return imsi;
	}

	public static String getUserAgent() {
		return userAgent;
	}

	public static int getScreenWidth() {
		return screenWidth;
	}

	public static int getScreenHeight() {
		return screenHeight;
	}

	public static String getCarrier() {
		return carrier;
	}
 
	
	public static String getModel()
	{
		return model;
	}
	
	public static String getBrand()
	{
		return brand;
	}

	public static int getVersion() {
		return version;
	}	
	
	
	
}
